

#include "Test.h"

test::test(QWidget *widget)
        : QWidget(widget)
{
    InitWidget();
    InitUi();
    InitState();
    InitEvent();

    now = QTime(12,0,0);
    int start = QTime(0,0,0).secsTo(now);

    chart.SetYRange(800, 1000);
    chart.SetYLabel("y test");
    chart.SetXRangeStart(start, 15 * 60); //15min
    chart.SetColor({{"Pplat", Qt::red}, {"Ppeak", Qt::blue}, {"PEEP", Qt::black}, {"Pmean", Qt::yellow}, {"SEV", Qt::green}});
    chart.resize(600, 500);
    chart.show();

    verHeader = QStringList{"Pplat", "Ppeak", "PEEP", "Pmean", "SEV"};
    table.SetVerHeader(verHeader);
    table.resize(600, 500);
    table.show();


//    GenerateData();
}

void test::InitUi()
{
    auto hbox = new QHBoxLayout(this);
    hbox->addWidget(hour);
    hbox->addWidget(customColor);
    hbox->addWidget(resetRange);
    hbox->addWidget(testLBtn);
    hbox->addWidget(testRBtn);
    hbox->addWidget(startBtn);
    hbox->addWidget(stopBtn);
    hbox->addWidget(clearBtn);
    hbox->addWidget(addLineBtn);
    hbox->addWidget(delLineBtn);
    hbox->addWidget(combo);
}

void test::InitEvent()
{
    static int count = 0;
    connect(testLBtn, &QPushButton::clicked, this, [=](){
        if(count >= verHeader.size())
            return ;

        chart.AddGraph(verHeader[count++], CTrendChart::PosLeft);
        chart.Update();
    });

    connect(testRBtn, &QPushButton::clicked, this, [=](){
        if(count >= verHeader.size())
            return ;

        chart.SetYRange(800, 1000, 100, 200);
        chart.SetYLabel("y test", "y2 test");
        chart.AddGraph(verHeader[count++], CTrendChart::PosRight);
        chart.Update();
    });

    connect(startBtn, &QPushButton::clicked, this, [=](){

        GenerateData(hour->value());
        qDebug() << "--------" << hour->value() << "h";
        QElapsedTimer timer;
        timer.start();
        table.Clear();
        table.SetData(dataSet);
        qDebug() << "table set data:" << timer.elapsed() << "ms";

        timer.restart();
        chart.SetData(dataSet);
        chart.Update();
        qDebug() << "chart set data:" << timer.elapsed() << "ms";

    });

    connect(stopBtn, &QPushButton::clicked, this, [=](){
        timer.stop();
    });

    connect(clearBtn, &QPushButton::clicked, this, [=](){
        table.Clear();

        chart.ClearGraph();
        chart.RemoveLine(last_line);
    });

    connect(resetRange,&QPushButton::clicked,this,[=](){

        auto now = QTime::currentTime().msecsSinceStartOfDay() / 1000.0;
        chart.SetXRangeStart(now, 15 * 60); //15min
        chart.SetYRange(800, 1000, 20, 100);
        chart.AdjustAxis();
        chart.Update();
    });

    connect(addLineBtn,&QPushButton::clicked,this,[=](){
        int value = QRandomGenerator::global()->bounded(500,600);
        auto orientation = QRandomGenerator::global()->bounded(0,1) ? Qt::Vertical : Qt::Horizontal;
        auto pos = QRandomGenerator::global()->bounded(0,1) ? CTrendChart::PosLeft : CTrendChart::PosRight;

        qDebug() << value << orientation << pos;
        last_line = chart.AddLine(value, Qt::yellow, orientation, pos);
        chart.SetLineVisible(last_line, true);
    });

    connect(delLineBtn,&QPushButton::clicked,this,[=](){

        if(last_line.isEmpty())
            return ;

        chart.RemoveLine(last_line);
        last_line.clear();
    });

    connect(&timer,&QTimer::timeout,this,[=](){

        auto time = QTime::currentTime();
        int s = QTime(0,0,0).secsTo(time);

        QMap<QString, double> datas;
        for(auto name : verHeader){
            int value = QRandomGenerator::global()->bounded(0,100);
            datas.insert(name,value);
        }

        table.AppendData(s, datas);
        chart.AppendData(s, datas);
    });

    connect(combo,static_cast<void (QComboBox::*)(int)>(&QComboBox::activated),this,[=](int index){
        int interve = combo->itemData(index).toInt();

        auto start = dataSet.first()->time;
        auto newData = QtConcurrent::blockingFiltered(dataSet, [=](const TrendDataSPtr &point) {
            return (point->time - start) % interve == 0;
        });

        QElapsedTimer timer;
        timer.start();
        table.Clear();
        table.SetData(newData);
        qDebug() << "table set data:" << timer.elapsed() / 1000.0 << "s";

        timer.restart();
        chart.SetData(newData);
        qDebug() << "chart set data:" << timer.elapsed() / 1000.0 << "s";
    });
}

void test::InitWidget()
{
    resetRange = new QPushButton("重置");
    customColor = new QPushButton("设置颜色");
    testLBtn = new QPushButton("测试-添加(左)");
    testRBtn = new QPushButton("测试-添加(右)");
    startBtn = new QPushButton("开始");
    stopBtn = new QPushButton("结束");
    clearBtn = new QPushButton("清除");
    addLineBtn = new QPushButton("加线");
    delLineBtn = new QPushButton("减线");
    combo = new QComboBox();
    hour = new QSpinBox();
}

void test::InitState()
{
    timer.setSingleShot(false);
    combo->addItem("1min",1 * 60);
    combo->addItem("1.5min",1.5 * 60);
    combo->addItem("5min",5 * 60);
    combo->addItem("10min",10 * 60);
    combo->addItem("15min",15 * 60);
    combo->addItem("30min",30 * 60);
    combo->addItem("1h",60 * 60);
    combo->addItem("2h",2 * 60 * 60);
}

void test::GenerateData(int hour)
{
    dataSet.clear();
    int dur = hour * 60 * 60 ; //24h 间隔30s

    QVector<int> init(verHeader.size());
    for (int i = 0; i < verHeader.size(); ++i)
        init[i] = 10 * i;


    bool first = true;
    for (int i = 0; i < dur; ++i){
        if(i % 30 != 0)
            continue;

        auto ptr = std::make_shared<TrendData>();
        ptr->time = i;
        for (int j = 0; j <verHeader.size() ; ++j){
            auto name = verHeader[j];
            ptr->dataSet.insert(name,first ? init[j] - 5 : init[j] + 5);
            first = !first;
        }
        dataSet << ptr;
    }
}
